package com.atguigu.sort;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * @Author : SunFeng
 * @Description: 希尔排序
 * @Date : 2021/7/1  16:27
 */
public class ShellSort {
    public static void main(String[] args) {
//        int[] array = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
//        shellSort(array);
//        System.out.println(Arrays.toString(array));

        int[] array = new int[80000];
        for (int i = 0; i < 80000; i++) {
            array[i] = (int) (Math.random() * 8000000);
        }
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        shellSort2(array);
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(Arrays.toString(array));

    }

    /**
     * @param array 希尔排序 移位算法
     */
    public static void shellSort2(int[] array) {

        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                int j = i;
                int temp = array[j];
                while (j - gap >= 0 && temp < array[j - gap]) {
                    array[j] = array[j - gap];
                    j -= gap;
                }
                array[j] = temp;
            }
        }

    }


    /**
     * @param array 希尔排序 交换算法
     */
    public static void shellSort(int[] array) {
        int temp = 0;
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (array[j] > array[j + gap]) {
                        temp = array[j];
                        array[j] = array[j + gap];
                        array[j + gap] = temp;
                    }
                }
            }

        }



    /*
        for (int i = 5; i < array.length; i++) {

                for (int j = i - 5; j >= 0; j -= 5) {

                    if (array[j] > array[j + 5]) {
                        temp = array[j];
                        array[j] = array[j + 5];
                        array[j + 5] = temp;
                    }
                }
            }
    System.out.println(Arrays.toString(array));
        for (int i = 2; i < array.length; i++) {

            for (int j = i - 2; j >= 0; j -= 2) {

                if (array[j] > array[j + 2]) {
                    temp = array[j];
                    array[j] = array[j + 2];
                    array[j + 2] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
        for (int i = 1; i < array.length; i++) {

            for (int j = i - 1; j >= 0; j -= 1) {

                if (array[j] > array[j + 1]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));*/
    }
}
